package csu.web.mypetstore.persistence.impl;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Category;
import csu.web.mypetstore.domain.Logger;
import csu.web.mypetstore.persistence.DBUtil;
import csu.web.mypetstore.persistence.LoggerDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class LoggerDaoImpl implements LoggerDao  {
    private static final String SET_LOGGER="" +
            "insert into logger (username,action,date) values(?,?,?)";

    private static final String GET_LOGGER_LIST =
            "SELECT USERNAME , ACTION, DATE FROM LOGGER ";

    @Override
    public List<Logger> getLoggerList(){
        List<Logger> loggerList = new ArrayList<>();
        try{
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(GET_LOGGER_LIST);
            while(resultSet.next()){
                Logger logger = new Logger(resultSet.getString("username"),resultSet.getString("action"),resultSet.getString("date"));
                loggerList.add(logger);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
        return loggerList;
    }
    @Override
    public void setLogger(Logger logger) {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(SET_LOGGER);
            preparedStatement.setString(1, logger.getUsername());
            preparedStatement.setString(2, logger.getAction());
            preparedStatement.setString(3, logger.getDate());
            preparedStatement.execute();

            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
